#ifndef __peripheral_h
#define __peripheral_h

#define ioi             1

#define od		        0
#define doubled		    1
#define bug             0
#define bug_a1          0
#define usedef          1

#define	load_get_dl		0

#define kuai_8ms        0

#define en_di           0
#define PS1101          0
#define AD18F128        0

#define zhong_wei       1


#define cpu_t2          1


#include "public.h"



#define cmpout_pwm              CMP2OUT
#define cmpie_pwm               CMP2IE
#define cmpif_pwm               CMP2IF
#define cmpon_pwm()             cmpif_pwm=0;cmpie_pwm=1
#define cmpoff_pwm()            cmpie_pwm=0;cmpif_pwm=0

#define tim_isr_pwm()           
#define timif_pwm               TMR2IF
#define timie_pwm               TMR2IE
#define timon_pwm()             TMR2ON=1
#define timoff_pwm()            TMR2ON=0
#define init_duty_t(x,y)        x=y*2-1
#define tim_init_pwm(x)         PR2=x; TMR2=0; T2CON=0x4f;timif_pwm=0;timie_pwm=1;PEIE=1;GIE=1

#if doubled
#define pwm_dutyoff()	pwm.duty=0;pwm.duty1=0
#else
#define pwm_dutyoff()	pwm.duty=0
#endif

#if doubled
#define SMO_PWM1         RB3
#define SMO_DET1         RB2
#if od
#define od_pwm1			ODCONB3	
#endif

#define cmpout_pwm1              CMP1OUT
#define cmpie_pwm1               CMP1IE
#define cmpif_pwm1               CMP1IF
#define cmpon_pwm1()             cmpif_pwm1=0;cmpie_pwm1=1
#define cmpoff_pwm1()            cmpie_pwm1=0;cmpif_pwm1=0

#define tim_isr_pwm1()           
#define timif_pwm1               TMR1IF
#define timie_pwm1               TMR1IE
#define timon_pwm1()             TMR1ON=1
#define timoff_pwm1()            TMR1ON=0
#define init_duty_t1(x,y)        x=(65535u-(u16)y*160u)+1;
#define tim_init_pwm1(x)         T1CON=0x31;TMR1H=x>>8;TMR1L=(u8)x; timif_pwm1=0;timie_pwm1=1;PEIE=1;GIE=1

#endif



#define tim_cnt_isr()           
#define timon_cnt()             TMR2ON=1
#define timoff_cnt()            TMR2ON=0
#define timif_cnt               TMR2IF
#define timie_cnt               TMR2IE
#define tim_cnt_init()          TMR2=0;PR2=0x7c;T2CON=0xfb;timif_cnt=0;timie_cnt=1;PEIE=1;GIE=1;timon_cnt()

#define bug_pin_dire        TRISA4
#define bug_pin             RA4

#define TX_DIR  TRISA4
#define TX_PIN  RA4
/*
#define pols0	0xff
#define pols1	0xc0
#define pols2	0xff
#define pols3	0xc0
#define pols4	0xff
#define pols5	0xc0
#define pols6	0x00
#define pols7	0x3f
#define pols8	0x00
#define pols9	0x7f

#if 0
#define pme0	0x01
#define pme1	0x02
#define pme2	0x04
#define pme3	0x08
#define pme4	0x10
#define pme5	0x20
#define pme6	0x08
#define pme7	0x10
#define pme8	0x20
#else
#define pme0	HBPWMOE00
#define pme1	HBPWMOE01
#define pme2	HBPWMOE10
#define pme3	HBPWMOE11
#define pme4	HBPWMOE20
#define pme5	HBPWMOE21
#define pme6	HBPWMOE11
#define pme7	HBPWMOE20
#define pme8	HBPWMOE21
#define pme9	HBPWMOE20
#endif



#define duty0	DUTY20
#define duty1	DUTY20
#define duty2	DUTY22
#define duty3	DUTY22
#define duty4	DUTY24
#define duty5	DUTY24
#define duty6	DUTY22
#define duty7	DUTY24
#define duty8	DUTY24
#define duty9	DUTY24
*/

#define pwmduty0	PWMD0L
#define pwmduty1	PWMD1L
#define pwmduty2	PWMD2L
#define pwmduty3	PWMD3L
#define pwmduty4	PWMD4L
#define ledpwm__(x,y)	if(x<ui_ldmax){pwmduty##y=x<<ui_ldwy;pwmen##y=1;uidelay}

#define pwmen0	PWM0EN
#define pwmen1	PWM1EN
#define pwmen2	PWM2EN
#define pwmen3	PWM3EN
#define pwmen4	PWM4EN


#define SMO_PWM         RA0
#define SMO_DET         RA1
#if od 
#define od_pwm			ODCONA0	
#endif


#if load_get_dl
#define SMO_DET_EN      PB0
#endif 
#define SI_MIC          RA2
#define CHRG_FULL       RA3
#define USB_DET         RA4
#define key             RA6
#define LED0    RA5
#define LED1    RB7
#define LED2    RB6
#define LED3    RB5
#define LED4    RB4

/*
#define LED5    PC5
#define LED6    PB5
#define LED7    PB4
#define LED8    PB3
#define LED9    PB2
*/
#define tris0    TRISA5
#define tris1    TRISB7
#define tris2    TRISB6
#define tris3    TRISB5
#define tris4    TRISB4
/*
#define tris5    TRISC5
#define tris6    TRISB5
#define tris7    TRISB4
#define tris8    TRISB3
#define tris9    TRISB2
*/

#ifndef uint8_t
#define uint8_t		unsigned char 
#define uint16_t 	unsigned int  
#define uint32_t 	unsigned long 
#endif

#ifndef u8
#define u8		unsigned char 
#define u16 	unsigned int  
#define u32 	unsigned long 
#endif

#ifndef s8
#define s8		signed char 
#define s16 	signed int  
#define s32 	signed long 
#endif

#ifndef vu8
#define vu8		volatile unsigned char 
#define vu16 	volatile unsigned int  
#define vu32 	volatile unsigned long 
#endif

void cmp_init(void);
void gpio_init(void);
void timer1_init(void);
void timer0_init(void);//500us
u16 adc_demo(u8 chl);//0 bat  1 load

void uart_init(void);//115200
void uart_txc(u8 c);
void uart_txnum(uint32_t p,uint8_t len);

void uart_txln(const char * p);
void timer2_init(void);
void pwmpin_init(void);

u16 get_bat(void);
u8 get_load(void);
void sleep_mode(void);
#if cpu_t2
#define adc__() ANS1=1;ANS10=1;\
				ADCON1=0x40;\
				ADCON0=0x7d;\
				ADIF=0;\
				ADIE=0;\
				ADCON0 |=(uint8_t)0x02;\
				while(ADIF!=1);\
				ADIF=0;\
				pwm.bat_t=((u16)((ADRESH<<8)|ADRESL)>>4);\
                \
				ADCON1=0x00;\
				ADCON0=0x45;\
				ADIF=0;\
				ADIE=0;\
				ADCON0 |=(uint8_t)0x02;\
				while(ADIF!=1);\
				ADIF=0;\
				pwm.load_t=((u16)((ADRESH<<8)|ADRESL)>>4);\
				\
				ADCON1=0x00;\
				ADCON0=0x69;\
				ADIF=0;\
				ADIE=0;\
				ADCON0 |=(uint8_t)0x02;\
				while(ADIF!=1);\
				ADIF=0;\
				pwm.load_t1=((u16)((ADRESH<<8)|ADRESL)>>4);\
				ANS1=0;ANS10=0
#else
#define adc__() ADCON1=0x40;\
				ADCON0=0x7d;\
				ADIF=0;\
				ADIE=0;\
				ADCON0 |=(uint8_t)0x02;\
				while(ADIF!=1);\
				ADIF=0;\
				pwm.bat_t=((ADRESH<<8)|ADRESL)>>4;\
                \
				ADCON1=0x00;\
				ADCON0=0x45;\
				ADIF=0;\
				ADIE=0;\
				ADCON0 |=(uint8_t)0x02;\
				while(ADIF!=1);\
				ADIF=0;\
				pwm.load_t=((ADRESH<<8)|ADRESL)>>4;\
				\
				ADCON1=0x00;\
				ADCON0=0x69;\
				ADIF=0;\
				ADIE=0;\
				ADCON0 |=(uint8_t)0x02;\
				while(ADIF!=1);\
				ADIF=0;\
				pwm.load_t1=((ADRESH<<8)|ADRESL)>>4

#endif
				



#define pin0_on	0x01
#define	pin1_on 0x02
#define pin2_on 0x04
#define pin3_on 0x08
#define pin4_on 0x10
#define pin5_on 0x20
#define pin6_on 0x40
#define	pin7_on 0x80

#define pin0_off	0	
#define	pin1_off	0
#define pin2_off	0
#define pin3_off	0
#define pin4_off	0
#define pin5_off	0
#define pin6_off	0
#define	pin7_off	0


#endif